var defaultLat = 24.728821282887854;
var defaultLon = 118.64513897773577;
$(function() { 
	customerMap.add(function() {
		$('#map_canvas').gmap({'center': defaultLat+', '+defaultLon, 'zoom': 15, 'disableDefaultUI':true, 'callback': function(map) {
			var self = this;
			$.getJSON( website + "boSupplier/mapData/"+$("#curSupplier").val(), function(data) { 
				$.each( data, function(i, markerData) {
					var latitude = markerData.latitude;
					var longitude = markerData.longitude;
					if(markerData.supplier_map_address == ""){
						self.search({'address': markerData.supplier_address}, function(results, status) {
							if ( status === 'OK' ) {
								latitude = results[0].geometry.location.Ya;
								longitude = results[0].geometry.location.Za;
							}else{
								latitude = defaultLat;
								longitude = defaultLon;
							}
							self.addMarker({ 'position': new google.maps.LatLng(latitude, longitude), 'draggable': true, 'bounds':false }, function(map, marker) {
								appendDialog(marker);
								$('#suppliername'+marker.__gm_id).val(markerData.supplier_name);
								$('#longitude'+marker.__gm_id).val(marker.getPosition().lng());
								$('#latitude'+marker.__gm_id).val(marker.getPosition().lat());
								$('#supplier'+marker.__gm_id).val($("#curSupplier").val());
								self.get('findLocation')(marker.getPosition(), marker, false, markerData.supplier_name);
							}).dragend( function(event) {
								self.get('findLocation')(event.latLng, this, true, markerData.supplier_name);
							}).click( function() {
								self.get('openDialog')(this);
							})
						});
					}else{
						self.addMarker({ 'position': new google.maps.LatLng(latitude, longitude), 'draggable': true, 'bounds':false }, function(map, marker) {
							appendDialog(marker);
							$('#suppliername'+marker.__gm_id).val(markerData.supplier_name);
							$('#longitude'+marker.__gm_id).val(marker.getPosition().lng());
							$('#latitude'+marker.__gm_id).val(marker.getPosition().lat());
							$('#supplier'+marker.__gm_id).val($("#curSupplier").val());
							self.get('findLocation')(marker.getPosition(), marker, false, markerData.supplier_name);
						}).dragend( function(event) {
							self.get('findLocation')(event.latLng, this, true, markerData.supplier_name);
						}).click( function() {
							self.get('openDialog')(this);
						})
					}
				});
			});
			
			self.set('openDialog', function(marker) {
				$('#dialog'+marker.__gm_id).dialog({
					'modal':true, 
					'width':450,
					'title': '标记商户地址', 
					'buttons': { 
						'标记': function() { 
							var params = {
								supplierid:$("#curSupplier").val(),
								address : $('#address'+marker.__gm_id).val(),
								longitude:$('#longitude'+marker.__gm_id).val(),
								latitude:$('#latitude'+marker.__gm_id).val()
							}
							mask.open(true);
							ajaxGet(website + "boSupplier/markMap",params,function(result){
								getPage($("#currentPage").val());
								mask.close();
							},"html");
							$("#dialog"+marker.__gm_id).dialog('close'); 
							return false; 
						},
						'移出': function() { 
							marker.setMap(null); 
							$('#address'+marker.__gm_id).val("");
							$('#longitude'+marker.__gm_id).val("");
							$('#latitude'+marker.__gm_id).val("");
							var params = {
								supplierid:$("#curSupplier").val(),
								address : $('#address'+marker.__gm_id).val(),
								longitude:$('#longitude'+marker.__gm_id).val(),
								latitude:$('#latitude'+marker.__gm_id).val()
							}
							mask.open(true);
							ajaxGet(website + "boSupplier/markMap",params,function(result){
								getPage($("#currentPage").val());
								mask.close();
							},"html");
							$("#dialog"+marker.__gm_id).dialog('close'); 
							return false; 
						}
					}
				});
			});
			self.set('findLocation', function(location, marker, isHopup, title) {
				self.search({'location': location}, function(results, status) {
					if ( status === 'OK' ) {
						$.each(results[0].address_components, function(i,v) {
							if ( v.types[0] == "administrative_area_level_1" || v.types[0] == "administrative_area_level_2" ) {
								$('#state'+marker.__gm_id).val(v.long_name);
							} else if ( v.types[0] == "country") {
								$('#country'+marker.__gm_id).val(v.long_name);
							}
						});
						if(title != null){
							marker.setTitle(title + "\n" + results[0].formatted_address);
						}else{
							marker.setTitle(results[0].formatted_address);
						}
						$('#address'+marker.__gm_id).val(results[0].formatted_address);
						$('#longitude'+marker.__gm_id).val(location.lng());
						$('#latitude'+marker.__gm_id).val(location.lat());
						if(isHopup){
							self.get('openDialog')(marker);
						}
					}
				});
			});
			
			$(map).click( function(event) {
				self.addMarker({'position': event.latLng, 'draggable': true, 'bounds': false}, function(map, marker) {
					appendDialog(marker);
					$('#supplier'+marker.__gm_id).val($("#curSupplier").val());
					self.get('findLocation')(marker.getPosition(), marker, true, null);
				}).dragend( function(event) {
					self.get('findLocation')(event.latLng, this, true, null);
				}).click( function() {
					self.get('openDialog')(this);
				})
			});
		}});
	}).load();
});

function appendDialog(marker){
	$('#dialog').append(
			'<form id="dialog'
					+ marker.__gm_id
					+ '" method="post" action="'+website+'boSupplier/markMap" style="display:none;"><p><label for="country">国家</label><input id="country'
					+ marker.__gm_id
					+ '" style="width:100%" name="country" value=""/></p><p><label for="state">省份</label><input id="state'
					+ marker.__gm_id
					+ '" style="width:100%" name="state" value=""/></p><p><label for="address">地址</label><input id="address'
					+ marker.__gm_id
					+ '" style="width:100%" name="address" value=""/></p><p><label for="longitude">经度</label><input id="longitude'
					+ marker.__gm_id
					+ '" style="width:100%" name="longitude" value=""/></p><p><label for="latitude">纬度</label><input id="latitude'
					+ marker.__gm_id
					+ '" style="width:100%" name="latitude" value=""/></p><input id="supplier'
					+ marker.__gm_id
					+ '" type="hidden" name="supplierid" /></form>');
}